package com.smartcar.admin.service.impl;

import com.smartcar.admin.config.FastUtil;
import com.smartcar.admin.feign.ClientServiceFeign;
import com.smartcar.admin.mapper.AdminMapper;
import com.smartcar.admin.service.AdminService;
import com.smartcar.client.domin.Annex;
import com.smartcar.client.domin.Vip;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.FindUserVo;
import com.smartcar.common.domain.request.UpdateLoginVo;
import com.smartcar.common.utils.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private FastUtil fastUtil;
    @Autowired
    private ClientServiceFeign clientServiceFeign;

    @Override
    public User findUserByUserName(FindUserVo findUserVo) {
        return adminMapper.findUserName(findUserVo);
    }

    @Override
    //@Transactional
    public int insertUser(User user) {
        int i = adminMapper.insertUser(user);
        if (user.getRoleId() == 1) {
            Annex annex = Annex.builder()
                    .userId(Long.valueOf(user.getId()))
                    .annexNumber(SnowFlakeUtil.getDefaultSnowFlakeId() + "_" + System.currentTimeMillis())
                    .annexAvailableMoney(BigDecimal.ZERO)
                    .annexFreezeMoney(BigDecimal.ZERO)
                    .annexMayInvoice(BigDecimal.ZERO)
                    .annexNotInvoice(BigDecimal.ZERO)
                    .annexTotalMileage("0")
                    .annexMonthMileage("0")
                    .annexTotalPrice(BigDecimal.ZERO)
                    .annexRefundPrice(BigDecimal.ZERO)
                    .annexPayPrice(BigDecimal.ZERO)
                    .annexMonthPrice(BigDecimal.ZERO)
                    .build();
            clientServiceFeign.insertAnnex(annex);
            Vip vip = Vip.builder()
                    .userId(Long.valueOf(user.getId()))
                    .vipNumber(SnowFlakeUtil.getDefaultSnowFlakeId() + "_" + System.currentTimeMillis())
                    .vipName("vip")
                    .vipValue("0")
                    .vipStatus(1)
                    .vipPeople(user.getNickName())
                    .vipDate(new Date())
                    .vipRemark("无").build();

            clientServiceFeign.insertVip(vip);
        }
        return i;
    }

    @Override
    public User verifyUniqueUserName(String username) {
        return adminMapper.verifyUniqueUserName(username);
    }

    @Override
    public String uploadPicture(MultipartFile file) {
        String picturePath = "";
        try {
            picturePath = fastUtil.upload(file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "http://124.220.90.60:8888/" + picturePath;
    }

    @Override
    public User findByPhone(String phone) {

        User findByPhone = adminMapper.findByPhone(phone);
        return findByPhone;
    }

    @Override
    public int updateLogin(UpdateLoginVo updateLoginVo) {
        return adminMapper.updateLogin(updateLoginVo);
    }

    @Override
    public int updateDriverAcceptStatus(User user) {
        return adminMapper.updateDriverAcceptStatus(user);
    }

    @Override
    public int updateDriverCar(User user) {
//        return adminMapper.updateDriverCar(user);
        return 1;
    }

    @Override
    public int updateDriverAddr(User user) {
        return 0;
    }
}
